package com.bishe.demo.modules.other.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.dao.*;
import com.bishe.demo.modules.other.entity.Image;
import com.bishe.demo.modules.other.entity.Travel;
import com.bishe.demo.modules.other.entity.TravelImage;
import com.bishe.demo.modules.other.entity.UserTravel;
import com.bishe.demo.modules.other.service.TravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class TravelServiceImpl implements TravelService {
    @Autowired
    private TravelDao travelDao;

    @Autowired
    private SceneryTravelDao sceneryTravelDao;

    @Autowired
    private TravelImageDao travelImageDao;

    @Autowired
    private UserTravelDao userTravelDao;

    @Autowired
    private ImageDao imageDao;


    @Override
    @Transactional
    public ResultEntity<Travel> insertTravel(Travel travel) {
        travel.setCreateTime(LocalDateTime.now());
        travelDao.insertTravel(travel);
        userTravelDao.insertUserTravel(new UserTravel(travel.getUser().getUserId(), travel.getTravelId()));
        List<Image> images = travel.getImages();
        if (images != null && images.size() > 0) {
            for (Image image : images) {
                imageDao.insertImage(image);
                travelImageDao.insertTravelImage(new TravelImage(travel.getTravelId(), image.getImageId()));
            }
        }
        return new ResultEntity<Travel>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", travel);
    }

    @Override
    @Transactional
    public ResultEntity<Travel> updateTravel(Travel travel) {
        travelDao.updateTravel(travel);
        List<Image> images = travel.getImages();
        if (images != null && images.size() > 0) {
            travelImageDao.deleteTravelImageByTravelId(travel.getTravelId());

            for (Image image : images) {
                imageDao.deleteImageByImageId(image.getImageId());
                imageDao.insertImage(image);
                travelImageDao.insertTravelImage(new TravelImage(travel.getTravelId(), image.getImageId()));
            }
        }
        return new ResultEntity<Travel>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", travel);
    }

    @Override
    @Transactional
    public ResultEntity<Travel> deleteTravelByTravelId(int travelId) {
        List<Integer> ids = travelImageDao.getImagesIds(travelId);
        for (Integer id : ids) {
            imageDao.deleteImageByImageId(id);
        }
        travelDao.deleteTravelByTravelId(travelId);
        sceneryTravelDao.deleteSceneryTravelByTravelId(travelId);
        travelImageDao.deleteTravelImageByTravelId(travelId);
        userTravelDao.deleteUserTravelByTravelId(travelId);
        return new ResultEntity<Travel>(ResultEntity.ResultStatus.SUCCESS.code, "删除成功");
    }

    @Override
    public Travel getTravelByTravelId(int travelId) {
        return travelDao.getTravelByTravelId(travelId);
    }

    @Override
    public PageInfo<Travel> getTravelBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Travel>(Optional
                .ofNullable(travelDao.getTravelBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
